import { NestFactory } from '@nestjs/core'
import { app } from 'electron'
// import type { MicroserviceOptions } from '@nestjs/microservices'
// import { ElectronIpcTransport } from '@doubleshot/nest-electron'
import { AppModule } from './app.module'
import { readConfig } from './util/CommUtil'
import log from 'electron-log/main'
// import { setupSwagger } from './swaggerConfig'
import { setupCross } from './util/crossConfig'

log.initialize()

process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'

const configObj = readConfig()
console.log('配置文件信息', configObj)

async function electronAppInit() {
  const isDev = !app.isPackaged
  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
  })

  if (isDev) {
    if (process.platform === 'win32') {
      process.on('message', data => {
        if (data === 'graceful-exit') app.quit()
      })
    } else {
      process.on('SIGTERM', () => {
        app.quit()
      })
    }
  }

  await app.whenReady()
}

async function bootstrap() {
  try {
    await electronAppInit()

    const app = await NestFactory.create(AppModule)
    setupCross(app)
    await app.listen(configObj.httpServer.port)
  } catch (error) {
    // eslint-disable-next-line no-console
    console.log(error)
    app.quit()
  }
}

bootstrap()
